package cn.ulc.socket.admin.service.pubData.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.util.ConvertUtil;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.pubData.PubDataListDTO;
import cn.ulc.socket.admin.model.param.common.PubDataSaveParam;
import cn.ulc.socket.admin.service.pubData.PubDataServ;
import cn.ulc.store.dao.PubDataDao;
import cn.ulc.store.entity.PubData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PubDataServImpl extends BaseService implements PubDataServ {
    @Autowired
    PubDataDao pubDataDao;

    @Override
    public PubDataListDTO findListByType(Integer type) {
        List<PubData> list = pubDataDao.findListByType(type);
        return PubDataListDTO.builder().dataList(list).build();
    }

    @Override
    public String findCodeByTypeAndName(Integer dataType, String name) {
        return pubDataDao.findCodeByTypeAndName(dataType,name);
    }

    @Override
    public String findNameByTypeAndCode(Integer dataType, String code) {
        return pubDataDao.findNameByTypeAndCode(dataType,code);
    }

    @Override
    public PubData saveDetail(PubDataSaveParam param) {
        String letter = "";
        if(PubDataConstant.DATA_TYPE_ROLE.compareTo(param.getDataType())==0){
            letter = "R";
        }
        else if(PubDataConstant.DATA_TYPE_COUPON.compareTo(param.getDataType())==0){
            letter = "C";
        }
        else if(PubDataConstant.DATA_TYPE_BILL.compareTo(param.getDataType())==0){
            letter = "L";
        }
        else{
            letter = "X";
        }
        return save(param,letter);
    }
    /**
     * 保存 公共数据
     * @param param 表单
     * @param letter Code首字母
     * @return
     */
    public PubData save(PubDataSaveParam param, String letter){
        boolean isEdit = param.getId() != null && param.getId() > 0;
        PubData entity = null;
        if(isEdit){
            entity = pubDataDao.findOne(param.getId());
            if(entity == null) throw new PlatformException("未获取到数据");
        }else{
            entity = new PubData();
            entity.setDataType( param.getDataType() );
            String maxCode = pubDataDao.findMaxCode( param.getDataType() );
            if(StrUtil.isBlank(maxCode)) {
                entity.setCode(letter + "00001");
            }else{
                int num = Convert.toInt(maxCode.substring(1))+1;
                entity.setCode(letter + ConvertUtil.createCode(num,5));
            }
        }
        setBaseInfo(entity);
        entity.setName(param.getName());
        entity.setRemark(param.getRemark());
        entity.setIsInit(false);
        pubDataDao.save(entity);
        return entity;
    }

    @Override
    public Boolean deleted(Long id) {
        PubData entity = pubDataDao.findOne(id);
        if(entity != null){
            setBaseInfo(entity);
            entity.setDeleted(DeletedStatus.DELETED);
            pubDataDao.save(entity);
        }
        return true;
    }
}
